//
//  FeedBackViewController.m
//  yunyan
//
//  Created by haichuang on 2018/2/26.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "FeedBackViewController.h"
#import "BRPlaceholderTextView.h"
#define kMaxTextCount 500
@interface FeedBackViewController ()<UITextViewDelegate>
{
    
    float noteTextHeight;
}

@property (strong, nonatomic)  UILabel *wordNum;
@property (strong, nonatomic)  UIButton *submitBtn;


@property (nonatomic, strong) BRPlaceholderTextView *feedBackTextView;
@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
   [self customNaviItemTitle:@"意见反馈" isLeft:YES];
    
    _feedBackTextView = [[BRPlaceholderTextView alloc]init];
    [self.view addSubview:_feedBackTextView];
    WeakSelf;
    [_feedBackTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo (weakSelf.view.mas_left).offset (15.f);
        make.right.equalTo (weakSelf.view.mas_right).offset (-15.f);
        make.top.equalTo (weakSelf.view.mas_top).offset(16.0);
        make.height.equalTo (@(100.0));
            
        }];
  
    _feedBackTextView.delegate = self;
    _feedBackTextView.placeholder = @" 写下您对我们的建议或者期待 （500文字以内）";
    _feedBackTextView.maxTextLength = kMaxTextCount;
    _feedBackTextView.font = [UIFont systemFontOfSize:16];
    
   
    _submitBtn = [[UIButton alloc] init];
    [self.view addSubview:_submitBtn];
    [_submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo (_feedBackTextView.mas_left).offset (10.f);
        make.right.equalTo (_feedBackTextView.mas_right).offset (-10.f);
        make.height.equalTo (@(50.f));
        make.top.equalTo (_feedBackTextView.mas_bottom).offset (15.f);
    }];
    [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_submitBtn setTitle: @"提交" forState:UIControlStateNormal];
    _submitBtn.backgroundColor = RGBA(81, 151, 238, 1);
    _submitBtn.layer.masksToBounds = YES;
    [_submitBtn addTarget:self action:@selector(submitBtn:) forControlEvents:UIControlEventTouchUpInside];
    _submitBtn.layer.cornerRadius = 5;
    
    
}

-(void)submitBtn:(UIButton *)btn{
    
    if (_feedBackTextView.text.length==0||_feedBackTextView.text==nil) {
        
        SHuiMBhudTextTimeStr(@"请输入反馈意见");
        return;
    }
    
    if (_feedBackTextView.text.length<6) {
        
        SHuiMBhudTextTimeStr(@"意见反馈不低于6个字");
        return;
    }
    
        [self RequestForMessage:_feedBackTextView.text];
  
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
   
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    [self textChanged];
    return YES;
}

//文本框每次输入文字都会调用  -> 更改文字个数提示框
- (void)textViewDidChangeSelection:(UITextView *)textView{
  
    [self textChanged];
}
/**
 *  文本高度自适应
 */
-(void)textChanged{
    
    CGRect orgRect = self.feedBackTextView.frame;//获取原始UITextView的frame
    
    //获取尺寸
    CGSize size = [self.feedBackTextView sizeThatFits:CGSizeMake(self.feedBackTextView.frame.size.width, MAXFLOAT)];
    
    orgRect.size.height=size.height+10;//获取自适应文本内容高度
    
    
    //如果文本框没字了恢复初始尺寸
    if (orgRect.size.height > 100) {
        noteTextHeight = orgRect.size.height;
    }else{
        noteTextHeight = 100;
    }
    
    [_feedBackTextView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo (@(noteTextHeight));
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

-(void)RequestForMessage:(NSString  *)messge{
    
    NSDictionary * dic = @{
                           @"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"mac_code":[SHJudgeTools getDeviceId],
                           @"content": messge
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(faceBack_message_api) andparameters:dic Success:^(id responseObject) {
        
        SHuiMBhudTextTimeStr(@"反馈成功！");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404) {
            SHuiMBhudTextTimeStr(@"未获取到数据");
        }else {
            SHuiMBhudTextNetError;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

